import { defineStore } from "pinia";
import { LoginApi,getUserInfoApi} from "../api/user";
import { ConsoleSqlOutlined } from "@ant-design/icons-vue";

const useUserStore = defineStore("user",{
    state:()=>({
       token:"",
       info:null,
    }),
    // 方法：同步和异步
    actions: {
    // 1.发送请求获取到数据 存储 token
    async getUserToken({ username,password }){
        try {
        const res = await LoginApi({ username,password });
        // 存储token
        this.token = res.access_token;
        console.log(this.token);
        this.getUserInfo();



            return Promise.resolve(true);
        } catch (error) {
            return Promise.reject(false);

        }
     },
// 获取用户信息
async getUserInfo(){
     try {
        const res = await getUserInfoApi(this.token);
        console.log(res);
        // 存储
        this.UserInfo = res;
        return Promise.resolve(true);

     } catch (error) {
        console.log(error);
        return Promise.reject(false);
        
     }


// console.log("getUser");
}

    },
    // 计算属性
    getters: { },
    persist:{
        enabled:true,
        strategies:[
            {
                key :"user1",
                storage:localStorage,
            }
        ]
    }
});
export default useUserStore;
